import { getHomeworkDetail, saveHwQuestionSubmit } from '@/api/bussiness/homework/mineHomework'

const state = {
  hw: null,
  homeworkInfo: {
    id: 9,
    courseId: 2,
    homeworkName: '实验一',
    startTime: '2023-04-21 00:00:00',
    endTime: '2023-04-25 00:00:00'
  },
  questions: {
    chooseQues: [
      {
        id: 19,
        homeworkId: 9,
        title: '1+1=?',
        orderNum: 1,
        options: [
          '1',
          '2',
          '3',
          '4'
        ],
        isMultiple: false
      },
      {
        id: 20,
        homeworkId: 9,
        title: '3+5不等于？',
        orderNum: 2,
        options: [
          '8',
          '7',
          '6',
          '5'
        ],
        isMultiple: true
      }
    ],
    fillQues: [
      {
        id: 10,
        homeworkId: 9,
        title: '小明有10个苹果，吃了4个，还剩___个。',
        blankNum: 1,
        orderNum: 1
      },
      {
        id: 11,
        homeworkId: 9,
        title: '举头___，低头___。',
        blankNum: 2,
        orderNum: 2
      }
    ],
    answerQues: [
      {
        id: 15,
        homeworkId: 9,
        title: '完成上机实验，并提交代码和实验报告。',
        orderNum: 1
      }
    ]
  },
  status: 5,
  statusShow: '已批阅',
  submitInfo: {
    id: 1,
    stuNo: '20190102',
    studentName: '李伊美',
    homeworkId: 9,
    homeworkName: '实验一',
    createdTime: '2023-04-26 21:41:38',
    modifiedTime: '2023-04-26 21:41:38',
    questionSubmitList: [
      {
        id: 1,
        questionId: 19,
        questionType: 0,
        submitId: 1,
        content: '1',
        selectedOptions: [
          1
        ],
        blankAnswers: null
      },
      {
        id: 2,
        questionId: 20,
        questionType: 0,
        submitId: 1,
        content: '1,2,3',
        selectedOptions: [
          1,
          2,
          3
        ],
        blankAnswers: null
      },
      {
        id: 3,
        questionId: 10,
        questionType: 1,
        submitId: 1,
        content: '["6"]',
        selectedOptions: null,
        blankAnswers: [
          '6'
        ]
      },
      {
        id: 4,
        questionId: 11,
        questionType: 1,
        submitId: 1,
        content: '["望明月","思故乡"]',
        selectedOptions: null,
        blankAnswers: [
          '望明月',
          '思故乡'
        ]
      },
      {
        id: 5,
        questionId: 15,
        questionType: 2,
        submitId: 1,
        content: '实验一的报告和代码已发到邮箱',
        selectedOptions: null,
        blankAnswers: null
      }
    ]
  },
  checkInfo: {
    id: 1,
    submitId: 1,
    score: 8.9
  },
  questionCount: { // 题目统计信息
    chooseNum: null,
    fillNum: null,
    answerNum: null,
    totalNum: null
  }
}

const mutations = {

}

const actions = {
  refreshHwInfo({ state }, param) {
    return new Promise((resolve, reject) => {
      const { homeworkId, stuNo } = param
      getHomeworkDetail({ homeworkId, stuNo }).then(({ data }) => {
        state.hw = data
        // 计算题目统计信息
        if (state.hw.status !== 0) {
          const chooseNum = state.hw.questions.chooseQues.length
          const fillNum = state.hw.questions.fillQues.length
          const answerNum = state.hw.questions.answerQues.length
          state.questionCount = { totalNum: chooseNum + fillNum + answerNum, chooseNum, fillNum, answerNum }
        }
        // 持久化存储
        localStorage.setItem('hw', JSON.stringify(data))
        localStorage.setItem('hwCnt', JSON.stringify(state.questionCount))
        resolve(data)
      }).catch(() => { reject() })
    })
  },
  getQuesInfo({ state, commit }, param) {
    return new Promise((resolve, reject) => {
      if (!state.hw) {
        // 如果题目数据为空，从本地缓存获取题目数据
        state.hw = JSON.parse(localStorage.getItem('hw'))
        state.questionCount = JSON.parse(localStorage.getItem('hwCnt'))
      }
      const { qtype, qidx } = param
      const data = {}
      // 保存作业基本信息
      data.homework = {
        name: state.hw.homeworkInfo.homeworkName
      }
      // 根据题目类型qtype和题目索引获取题目信息
      if (qtype === 0) {
        data.question = state.hw.questions.chooseQues[qidx]
      } else if (qtype === 1) {
        data.question = state.hw.questions.fillQues[qidx]
      } else if (qtype === 2) {
        data.question = state.hw.questions.answerQues[qidx]
      } else {
        reject()
      }
      // 获取提交信息
      if (state.hw.submitInfo) {
        const sInfo = state.hw.submitInfo.questionSubmitList.find(s => s.questionType === qtype && s.questionId === data.question.id)
        data.submit = sInfo
      }
      resolve(data)
    })
  },
  // 保存单个题目作答结果
  saveChosseQuesSubmit({ state }, param) {
    return new Promise((resolve, reject) => {
      const { quesParam, submit } = param

      // 更新远程提交信息
      const reqData = {
        stuNo: quesParam.stuNo,
        homeworkId: state.hw.homeworkInfo.id,
        questionId: submit.questionId,
        questionType: submit.questionType,
        submitId: state.hw.submitInfo == null ? null : state.hw.submitInfo.id,
        content: submit.content,
        selectedOptions: submit.selectedOptions,
        blankAnswers: submit.blankAnswers
      }
      saveHwQuestionSubmit(reqData).then(({ data }) => {
        // 更新本地提交信息
        if (!state.hw.submitInfo) {
          state.hw.submitInfo = {
            id: data,
            questionSubmitList: []
          }
        }
        let flag = false
        for (let i = 0; i < state.hw.submitInfo.questionSubmitList.length; i++) {
          const sub = state.hw.submitInfo.questionSubmitList[i]
          if (sub.questionId === submit.questionId && sub.questionType === submit.questionType) {
            state.hw.submitInfo.questionSubmitList[i] = submit
            flag = true
            break
          }
        }
        if (!flag) {
          state.hw.submitInfo.questionSubmitList.push(submit)
        }
        // 更新本地提交缓存
        localStorage.setItem('hw', JSON.stringify(state.hw))
        resolve()
      }).catch((err) => { reject(err) })
    })
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

